package com.liyujie.networkrequest.pop;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;
import com.dyhdyh.support.countdowntimer.CountDownTimerSupport;
import com.dyhdyh.support.countdowntimer.OnCountDownTimerListener;
import com.liyujie.networkrequest.R;
import com.liyujie.networkrequest.utils.InitializeUtils;
import com.liyujie.networkrequest.utils.serialport.OpenPortUtils;
import com.lxj.xpopup.core.CenterPopupView;

public class DoorOpeningNumberSettingPopup extends CenterPopupView {

    private CountDownTimerSupport mTimernumber = new CountDownTimerSupport(2000, 1000);

    public DoorOpeningNumberSettingPopup(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {

        return R.layout.popup_door_openimg_number_setting;

    }


    @Override
    protected void onCreate() {
        super.onCreate();
        //开始编号
        EditText et_Start = findViewById(R.id.et_Start);
        //结束编号
        EditText et_End = findViewById(R.id.et_End);

        mTimernumber.setOnCountDownTimerListener(new OnCountDownTimerListener() {
            @Override
            public void onTick(long millisUntilFinished) {

            }

            @Override
            public void onFinish() {
                //结束
                final int numberEnd = Integer.parseInt(et_End.getText().toString());
                if(OpenPortUtils.sendData((byte) numberEnd,"2")){
                    InitializeUtils.getDefaultMaker().show("结束锁板编号成功");
                    dismiss();
                }else {
                    InitializeUtils.getDefaultMaker().show("发送数据失败");
                }
            }
        });

        //取消
        findViewById(R.id.quXiao).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();

            }
        });
        //确认
        findViewById(R.id.queRen).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(TextUtils.equals(et_Start.getText().toString(),"")){
                    ToastUtils.showShort("请输入开始的编号");
                    return;
                }
                if(TextUtils.equals(et_End.getText().toString(),"")){
                    ToastUtils.showShort("请输入结束的编号");
                    return;
                }
                //开始
                final int numberStart = Integer.parseInt(et_Start.getText().toString());
                //结束
                final int numberEnd = Integer.parseInt(et_End.getText().toString());
                if(numberStart > numberEnd){
                    InitializeUtils.getDefaultMaker().show("开始编号不能大于结束");
                    return;
                }
                InitializeUtils.getDefaultMaker().show("设置开始编号");
                if(OpenPortUtils.sendData((byte) numberStart ,"1")){
                    if(null != mTimernumber){
                        //倒计时开始
                        mTimernumber.reset();
                        mTimernumber.start();
                    }
                    InitializeUtils.getDefaultMaker().show("2秒后设置结束锁板编号");
                }else {
                    InitializeUtils.getDefaultMaker().show("发送数据失败");
                }
            }
        });
    }
}
